<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="500"
    :before-close="cancel"
  >
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
    >
      <el-form-item label="字典类型" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          保 存
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';

const { name, type } = defineProps(['type']);
const emit = defineEmits(['cancel']);

onMounted(() => {
  if (type === 'edit') {
    title.value = '编辑字典类型';
    ruleForm.name = name;
  }
});

const dialogVisible = ref(true);
const title = ref('新增字典类型');
const ruleFormRef = ref();
const ruleForm = reactive({
  name: 'Hello',
});
const rules = reactive({
  name: [
    { required: true, message: '请输入活动名称', trigger: 'blur' },
    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
  ],
});
const submitForm = async (ruleFormRef) => {
  await ruleFormRef.validate((valid, fields) => {
    if (valid) {
      console.log('submit!');
    } else {
      console.log('error submit!', fields);
    }
  });
};
const cancel = (flag = false) => {
  emit('cancel', flag);
};
</script>

<style lang="less" scoped>
.search-card,
.table-card {
  margin-bottom: 20px;

  .el-card__header {
    padding: 15px 20px;
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .header-buttons {
    display: flex;
    gap: 10px;
  }
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.text-muted {
  font-size: 13px;
  color: #909399;
  margin-top: 5px;
}
</style>
